from cProfile import label
from operator import le
import re
import pickle
from tqdm import tqdm
import re
import random

def step2(file_path):
    pre = set()
    result_sent, sent, record, idx, cixing = [], [], [], [], []
    count = 0
    label = set()
    with open(file_path,'r',encoding='gbk',errors='ignore') as file:
        content = file.readlines()
        for line in tqdm(content):
            ret = re.search('(\<\d+\>)(.+)',line.strip())
            if ret:
                flag = 0
                count = ret.group(1)
                sentence = ret.group(2)
                pos = sentence.split(' ')
                fenci = re.sub('\/[a-zA-Z]*','',sentence).split(' ')
                sent.append(''.join(i for i in fenci))
                num,i=[],0   
                for k in fenci:
                    num.append(list(range(i,i+len(k))))
                    i=i+len(k) 
            elif line.strip() == "====PGraph Beg====":
                continue
            elif line.strip() == "====PGraph End====":
                if flag == 0:
                    sent.append(num)
                    sent.append(idx)
                    sent.append(record)
                    sent.append(pos)
                    sent.append(cixing)
                    result_sent.append(sent)
                else:
                    flag = 0
                sent, record, idx, cixing = [], [], [], []
            elif len(line.strip()) != 0:
                head = line.strip().split(',')
                label.add(head[-1])
                pre.add(head[0])
                # if head[-1] in ['sbj_HX', 'obj_HX']:
                record.append((head[0],head[-1],head[2]))
                x = head[1].strip('()').split(':')
                y = head[3].strip('()').split(':')
                if len(set(x)) == 1 :
                    h = [int(i) for i in x[1:]]
                else:
                    m, n = int(x[0]), int(x[1])
                    h = list(range(m, m+(n-m)+1))
                if len(set(y)) == 1 :
                    t = [int(i) for i in y[1:]]
                else:
                    m, n = int(y[0]), int(y[1])
                    t = list(range(m, m+(n-m)+1))
                if (h in num) and (t in num):
                    idx.append((num.index(h),head[-1],num.index(t)))
                    cixing.append((pos[num.index(h)].split('/')[1],head[-1], pos[num.index(t)].split('/')[1]))
                else:  #re.search('\/([a-zA-Z]*)',st)
                    flag = 1
    print(label)
    print(len(pre))
    return result_sent

'''
[
'荆毅定居城市的第一件事，是给三岁的女儿找幼儿园，妻命我去寻。', 
[[0, 1], [2, 3], [4, 5], [6], [7, 8], [9], [10], [11], [12], [13], [14], [15], [16], [17, 18], [19], [20, 21, 22], [23], [24], [25], [26], [27], [28], [29]], 
[(8, 'sbj_HX', 6), (8, 'obj_HX', 14), (14, '_BY', 13), (14, 'obj_HX', 15), (18, 'sbj_HX', 17), (18, 'obj_HX', 19), (21, 'sbj_HX', 19)], 
[('是', 'sbj_HX', '事'), ('是', 'obj_HX', '找'), ('找', '_BY', '女儿'), ('找', 'obj_HX', '幼儿园'), ('命', 'sbj_HX', '妻'), ('命', 'obj_HX', '我'), ('寻', 'sbj_HX', '我')], 
['荆毅/nr', '定居/v', '城市/n', '的/u', '第一/m', '件/q', '事/n', '，/w', '是/v', '给/p', '三/m', '岁/q', '的/u', '女儿/n', '找/v', '幼儿园/n', '，/w', '妻/Ng', '命/n', '我/r', '去/v', '寻/v', '。/w']
[('v','sbj_HX','v'), ('v','sbj_HX','n')。。。]
]
'''

if __name__=="__main__":
    path = "../train_data/manual_data.out"  # 人工标注的数据8.8w
    result_sent = step2(path)
    new_result = []
    for item in tqdm(result_sent):
        sent = []
        sent.append(item[0])
        sent.append(item[1])
        idx, record = [], []
        for id, word, cix in zip(item[2],item[3],item[-1]):
            idx.append((id[0], id[1], 'null', id[2]))
            record.append((word[0], word[1], 'null', word[2]))
        sent.append(idx)
        sent.append(record)
        sent.append(item[-2])
        new_result.append(sent)
    print("len(new_result): ", len(new_result), new_result[0])  # sum :85676
  
    train = '../manual_argument/train_data.pkl'
    val = '../manual_argument/val_data.pkl'
    with open(train,'wb') as out_file:  # 82182
        pickle.dump(new_result[:84000],out_file) 
    with open(val,'wb') as out_file:
        pickle.dump(new_result[84000:85000],out_file) 
    




















           
    
    

